﻿package
	{
	import flash.geom.Point;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Projectile extends MovieClip
		{
		var speed:int = 75;
		var speedX:int;
		var speedY:int;
		var destroy:Boolean = false;
		var rebound:int = 10;
		
		public function addEvent()
			{
			this.addEventListener(Event.ENTER_FRAME, run);
			}
		
		public function run(e:Event)
			{
			x += speedX;
			y += speedY;
			var worldMap:MovieClip = MovieClip(parent);
			var end:int = worldMap.numChildren;
			
			for (var i =0; 	i < end; i++)
				{
				var actor = worldMap.getChildAt(i);
				if (actor is Player && hit.hitTestObject(actor))
					{
					actor.takeDamage(speed*0.1);
					destroy = true;
					break;
					}
				}
			
			if (destroy || x> 1800 || x<-200 || y<-200 || y>1800)
				{
				if (destroy || rebound <= 0)
					{
					parent.removeChild(this);
					removeEventListener(Event.ENTER_FRAME, run);
					delete this;
					}
				else
					{
					rotation += 180+Player.signedRand(20);
					setAngle();
					rebound--;
					}
				}
			}
		
		public function setAngle()
			{
			var angle:Number = (rotation+90)*MainClass.toRadian;
			speedX = Math.cos(angle)*this.speed;
			speedY = Math.sin(angle)*this.speed;
			}
		}
	}